import copy
import math
import time


def compute_shift(sramd,placement,access):
    port = 0
    shift_num = 0
    for i in access:
        if i not in sramd:
            shift_num = shift_num + abs(placement.index(i)-port)
            port = placement.index(i)
    return shift_num


def compute_cost(ns,Nr,Nw,sram_d,Trr,Twr,Tsr,Ts,D):
    cost_sram = 0
    energy_sram = 0
    cost_RM = ns * Tsr
    energy_RM = ns * Ers
    # SRAM上的读写延迟
    for i in sram_d:
        cost_sram = cost_sram+(Nr[i]+Nw[i])*Ts
        energy_sram = energy_sram + (Nr[i]+Nw[i])*Es
    # RM上的读写延迟加移动延迟
    for i in D:
        if i not in sram_d:
            cost_RM = cost_RM + Nr[i]*Trr + Nw[i]*Twr
            energy_RM = energy_RM +  Nr[i]*Err + Nw[i]*Erw
    cost = cost_RM + cost_sram
    energy = energy_sram + energy_RM
    return cost,energy



def reduce_sram(sram_d,x,p0,access):
    N_needreduce =math.ceil(len(sram_d) * x)  # 需要减少的个数
    p00 = copy.deepcopy(p0)
    # SramToRm = sram_d[len(sram_d)-N_needreduce+1:] # 需要拿出来放回RM的
    sram_d1 = sram_d[:len(sram_d)-N_needreduce]    # SRAM上剩下的数据

    # 更新此时的Placement
    for i in sram_d1:
        if i in p00:
            p00.remove(i)
    ns=compute_shift(sram_d1, p00, access)   # 得到该情况下的总移动次数
    cost2,energy2= compute_cost(ns, Nr, Nw, sram_d1, Trr, Twr, Tsr, Ts, D)
    # print("%%%%%%%%%%%%%%%%%", p00, sram_d1)
    return cost2,energy2,ns,sram_d1



if __name__ == "__main__":

    # 配置SRAM和RM
    Trr= 3.78
    Twr = 10.23
    Tsr = 4.95    #RM的移动延迟
    Ts = 3.94   #SRAM的延迟
    Ar = 128/32   # RM存32个数的area是128，一个就是2
    As = 120  # SRAM 存一个数的area
    Err =337.62  # RM读的energy  单位是pJ
    Erw = 1140 # RM写的energy单位是pJ
    Ers = 328.62 #RM移动操作的energy单位是pJ
    Es = 226 # SRAM的energy单位是pJ
    fold = round(Twr/Ts,3)  # 一个写相当于几个移动,保留3位小数
    #print("fold",fold)

    file_path = "D:\XuRui\study\论文\实验工具及负载\负载\Mibench\mibenchTrace"

    rw_sequence = []
    with open(file_path + "//2cores_rwnum.txt",encoding='gb18030',errors='ignore')as file:
        for line in file.readlines():
            rw_sequence.append(line.replace(',','').split())
    # print("rw_sequence:",rw_sequence[2][1])
    access_sequence = []
    with open(file_path + "//2cores_num.txt",encoding='gb18030',errors='ignore')as file:
        for line in file.readlines():
            access_sequence.extend(line.split())
        access_sequence = list(map(int, access_sequence))


    D = list(set(access_sequence))

    # 统计每个数据被读被写的次数
    Nr = [0 for i in range(len(D)+1)]  # 读的次数
    Nw = [0 for i in range(len(D)+1)]  # 写的次数


    cost0 = 48786687.87   # FCFS 的延迟  不使用SRAM时
    cost1 = 2473825.009    # SRAM全部使用时的延迟

    Ns0 = 9819253  # FCFS 的移动次数  不使用SRAM时
    Ns1 = 468099   # SRAM全部使用时的移动次数
    # SRAM上的数据
    data_SRAM = [19, 1184, 1185, 20, 1465, 1501, 300, 336, 35, 1918, 17, 1210, 100, 248, 69, 1234, 1413, 1924, 102, 2167, 45, 1200, 1591, 1917, 1916, 1915, 744, 746, 745, 747, 1592, 1923, 1925, 33, 1267, 1206, 41, 49, 53, 175, 753, 353, 103, 1927, 354, 104, 1265, 355, 105, 356, 106, 1191, 57, 61, 1214, 1218, 65, 1230, 996, 752, 754, 422, 423, 1430, 26, 1222, 1226, 756, 265, 98, 96, 1263, 1261, 15, 1340, 1182, 1522, 1268, 357, 107, 1523, 358, 1269, 108, 1524, 359, 1270, 109, 360, 1525, 1271, 110, 1526, 361, 1272, 111, 1527, 362, 1273, 112, 1528, 363, 1274, 113, 1332, 1180, 1926, 332, 364, 1529, 114, 1275, 1530, 365, 1276, 115, 366, 1531, 1277, 116, 1532, 367, 1278, 117, 368, 1533, 1279, 118, 1534, 369, 1280, 119, 1535, 370, 1281, 120, 1536, 1282, 371, 121, 1537, 372, 1283, 122, 1538, 373, 1284, 123, 1539, 1285, 374, 124, 1540, 1286, 375, 125, 376, 1541, 1287, 126, 1542, 377, 1288, 127, 1543, 378, 1289, 128, 1544, 379, 1290, 129, 380, 130, 1545, 1291, 1546, 1292, 381, 131, 1547, 382, 132, 1293, 383, 1548, 1294, 133, 1549, 384, 1295, 134, 1550, 385, 135, 1296, 1551, 386, 1297, 136, 1552, 1298, 387, 137, 388, 1553, 138, 1299, 1554, 391, 1300, 139, 1555, 392, 1301, 140, 1600, 1556, 393, 1302, 141, 394, 1557, 1303, 142, 1560, 395, 1304, 143, 396, 1561, 144, 1305, 1562, 397, 1306, 145, 398, 1563, 146, 1307, 1564, 1308, 399, 147, 1565, 400, 148, 1309, 1566, 401, 1310, 149, 1567, 402, 1311, 150, 1568, 403, 1312, 151, 1569, 404, 1313, 152, 405, 1570, 1314, 153, 1571, 406, 1315, 154, 1572, 407, 1316, 155, 408, 1573, 1317, 156, 1574, 409, 1318, 157, 1575, 410, 1319, 158, 1576, 411, 1320, 159, 1577, 412, 1321, 160, 1578, 413, 1322, 161, 1928, 1929, 431, 427, 414, 1579, 162, 1323, 1580, 415, 1324, 163, 416, 1581, 1325, 164, 1582, 417, 1326, 165, 1583, 418, 1327, 166, 1584, 419, 1328, 167, 1585, 1329, 1586, 1330, 1587, 1331, 1588, 180, 1934, 2081, 1596, 1497, 345, 352, 421, 1514, 1521, 1590, 1141, 2312, 1930, 1931, 1932, 1933, 1935, 763, 302, 172, 761, 1481, 1467, 1198, 684, 1409, 299, 1464, 244, 774, 1345, 241, 1197, 1262, 1949, 1945, 778, 764, 178, 1343, 1344, 179, 1406, 765, 1853, 1936, 97, 1896, 759, 760, 762, 99, 176, 1341, 1264, 81, 79, 1337, 1937, 1866, 1246, 1244, 1947, 758, 757, 766, 767, 768, 769, 776, 727, 1867, 1868, 1869, 2311, 2308, 1946, 1921, 32, 750, 1142, 1229, 2313, 775, 89, 2314, 2075, 1092, 93, 25, 37, 40, 44, 1084, 21, 770, 771, 772, 773, 1938, 1939, 22, 23, 24, 1186, 1187, 1188, 1189, 27, 28, 29, 30, 1192, 1193, 430, 1196, 1940, 777, 1941, 1942, 1943, 1944, 697, 698, 699, 700, 1948, 1163, 316, 1599, 942, 1046, 1047, 909]
    # RM上的放置
    p0 =[1, 2, 3, 4, 1166, 1167, 1168, 1169, 5, 6, 7, 8, 9, 1170, 1171, 1172, 1173, 1174, 10, 11, 12, 13, 1175, 1176, 1177, 1178, 14, 16, 1179, 1181, 18, 1183, 1190, 1194, 1195, 31, 34, 1199, 36, 1201, 1202, 38, 39, 1203, 1204, 42, 1205, 1207, 43, 1208, 1209, 46, 47, 1211, 1212, 48, 50, 1213, 1215, 51, 1216, 52, 1217, 54, 1219, 55, 56, 1220, 1221, 58, 1223, 59, 1224, 60, 1225, 62, 1227, 63, 1228, 64, 66, 1231, 67, 68, 1232, 1233, 70, 1235, 71, 72, 1236, 1237, 73, 74, 1238, 1239, 75, 1240, 76, 1241, 77, 78, 1242, 1243, 80, 1245, 82, 1247, 83, 84, 1248, 1249, 85, 1250, 86, 87, 88, 1251, 1252, 1253, 90, 1254, 1255, 91, 1256, 92, 1257, 94, 1258, 1259, 95, 1260, 101, 1266, 168, 169, 170, 1333, 1334, 1335, 171, 1336, 173, 1338, 174, 1339, 177, 1342, 181, 182, 1346, 1347, 183, 184, 185, 1348, 1349, 1350, 186, 187, 188, 1351, 1352, 1353, 189, 190, 1354, 1355, 191, 192, 1356, 1357, 193, 1358, 194, 195, 196, 197, 1359, 1360, 1361, 1362, 198, 1363, 199, 1364, 200, 201, 202, 1365, 1366, 1367, 203, 204, 205, 1368, 1369, 1370, 206, 207, 208, 1371, 1372, 1373, 209, 210, 211, 1374, 1375, 1376, 212, 213, 214, 215, 1377, 1378, 1379, 1380, 216, 217, 218, 219, 1381, 1382, 1383, 1384, 220, 221, 1385, 1386, 222, 223, 224, 225, 226, 1387, 1388, 1389, 1390, 1391, 227, 1392, 228, 1393, 229, 1394, 230, 231, 1395, 1396, 232, 233, 234, 1397, 1398, 1399, 235, 236, 1400, 1401, 237, 238, 1402, 1403, 239, 240, 1404, 1405, 242, 243, 245, 246, 1407, 1408, 1410, 1411, 247, 1412, 249, 1414, 250, 251, 1415, 1416, 252, 1417, 253, 254, 1418, 1419, 255, 256, 1420, 1421, 257, 1422, 258, 1423, 259, 260, 1424, 1425, 261, 262, 1426, 1427, 263, 1428, 264, 1429, 266, 1431, 267, 1432, 268, 1433, 269, 270, 271, 1434, 1435, 1436, 272, 1437, 273, 274, 275, 276, 1438, 1439, 1440, 1441, 277, 278, 279, 280, 1442, 1443, 1444, 1445, 281, 1446, 282, 1447, 283, 284, 1448, 1449, 285, 286, 287, 288, 1450, 1451, 1452, 1453, 289, 1454, 290, 291, 292, 1455, 1456, 1457, 293, 294, 295, 296, 297, 1458, 1459, 1460, 1461, 1462, 298, 1463, 301, 303, 1466, 1468, 304, 305, 1469, 1470, 306, 1471, 307, 308, 309, 310, 1472, 1473, 1474, 1475, 311, 312, 313, 1476, 1477, 1478, 314, 315, 1479, 1480, 317, 318, 319, 320, 1482, 1483, 1484, 1485, 321, 322, 1486, 1487, 323, 1488, 324, 325, 326, 1489, 1490, 1491, 327, 1492, 328, 1493, 329, 1494, 330, 331, 1495, 1496, 333, 1498, 334, 335, 1499, 1500, 337, 338, 1502, 1503, 339, 340, 1504, 1505, 341, 1506, 342, 343, 1507, 1508, 344, 1509, 1510, 1511, 1512, 346, 1513, 347, 348, 349, 350, 351, 1515, 1516, 1517, 1518, 1519, 1520, 389, 390, 1558, 1559, 420, 1589, 424, 425, 426, 428, 1593, 429, 1594, 432, 1595, 1597, 1598, 433, 434, 435, 436, 1601, 437, 438, 439, 1602, 1603, 1604, 1605, 440, 441, 1606, 442, 443, 1607, 1608, 444, 445, 446, 1609, 1610, 1611, 447, 448, 449, 450, 1612, 1613, 1614, 1615, 451, 1616, 452, 453, 454, 455, 1617, 1618, 1619, 1620, 456, 457, 458, 459, 460, 1621, 1622, 1623, 1624, 1625, 461, 462, 463, 464, 465, 1626, 1627, 1628, 1629, 1630, 466, 467, 468, 469, 1631, 1632, 1633, 1634, 470, 1635, 471, 472, 473, 474, 1636, 1637, 1638, 1639, 475, 476, 477, 478, 479, 1640, 1641, 1642, 1643, 1644, 480, 481, 1645, 1646, 482, 1647, 483, 484, 485, 1648, 1649, 1650, 486, 487, 1651, 1652, 488, 489, 490, 1653, 1654, 1655, 491, 492, 493, 494, 1656, 1657, 1658, 1659, 495, 496, 497, 498, 1660, 1661, 1662, 1663, 499, 500, 501, 502, 1664, 1665, 1666, 1667, 503, 504, 1668, 1669, 505, 506, 507, 1670, 1671, 1672, 508, 509, 1673, 1674, 510, 511, 1675, 1676, 512, 513, 1677, 1678, 514, 515, 516, 517, 1679, 1680, 1681, 1682, 518, 519, 520, 521, 522, 1683, 1684, 1685, 1686, 1687, 523, 524, 525, 526, 1688, 1689, 1690, 1691, 527, 528, 529, 530, 1692, 1693, 1694, 1695, 531, 532, 533, 534, 535, 1696, 1697, 1698, 1699, 1700, 536, 1701, 537, 538, 1702, 1703, 539, 540, 541, 542, 543, 1704, 1705, 1706, 1707, 1708, 544, 545, 1709, 1710, 546, 547, 1711, 1712, 548, 1713, 549, 550, 551, 552, 1714, 1715, 1716, 1717, 553, 1718, 554, 555, 1719, 1720, 556, 557, 558, 559, 1721, 1722, 1723, 1724, 560, 1725, 561, 562, 1726, 1727, 563, 564, 565, 566, 567, 1728, 1729, 1730, 1731, 1732, 568, 569, 1733, 1734, 570, 1735, 571, 572, 573, 574, 575, 1736, 1737, 1738, 1739, 1740, 576, 577, 1741, 1742, 578, 579, 580, 1743, 1744, 1745, 581, 582, 583, 584, 585, 1746, 1747, 1748, 1749, 1750, 586, 1751, 587, 588, 1752, 1753, 589, 1754, 590, 591, 592, 593, 594, 1755, 1756, 1757, 1758, 1759, 595, 596, 597, 598, 1760, 1761, 1762, 1763, 599, 600, 601, 1764, 1765, 1766, 602, 603, 604, 1767, 1768, 1769, 605, 606, 1770, 1771, 607, 608, 609, 610, 1772, 1773, 1774, 1775, 611, 612, 613, 614, 1776, 1777, 1778, 1779, 615, 616, 617, 1780, 1781, 1782, 618, 619, 620, 621, 622, 1783, 1784, 1785, 1786, 1787, 623, 624, 625, 1788, 1789, 1790, 626, 627, 628, 1791, 1792, 1793, 629, 1794, 630, 1795, 631, 632, 633, 634, 1796, 1797, 1798, 1799, 635, 636, 637, 638, 639, 1800, 1801, 1802, 1803, 1804, 640, 641, 642, 643, 1805, 1806, 1807, 1808, 644, 645, 646, 647, 1809, 1810, 1811, 1812, 648, 1813, 649, 650, 651, 1814, 1815, 1816, 652, 653, 1817, 1818, 654, 655, 656, 657, 658, 1819, 1820, 1821, 1822, 1823, 659, 660, 661, 662, 663, 1824, 1825, 1826, 1827, 1828, 664, 665, 1829, 1830, 666, 667, 1831, 1832, 668, 669, 670, 1833, 1834, 1835, 671, 672, 673, 1836, 1837, 1838, 674, 675, 676, 1839, 1840, 1841, 677, 678, 679, 680, 681, 1842, 1843, 1844, 1845, 1846, 682, 683, 1847, 1848, 1849, 1850, 1851, 685, 1852, 686, 687, 688, 689, 690, 1854, 1855, 691, 692, 1856, 1857, 693, 694, 695, 696, 1858, 1859, 1860, 1861, 1862, 1863, 1864, 1865, 701, 702, 703, 704, 1870, 705, 706, 707, 1871, 1872, 708, 709, 710, 1873, 1874, 1875, 1876, 711, 1877, 1878, 1879, 1880, 712, 713, 714, 1881, 715, 716, 1882, 717, 718, 719, 1883, 1884, 1885, 1886, 720, 1887, 1888, 1889, 721, 722, 723, 1890, 1891, 1892, 724, 725, 1893, 726, 1894, 1895, 728, 729, 730, 731, 732, 1897, 733, 734, 735, 736, 1898, 1899, 1900, 1901, 737, 738, 1902, 1903, 739, 1904, 1905, 1906, 1907, 740, 1908, 1909, 1910, 741, 742, 743, 1911, 1912, 1913, 748, 1914, 749, 1919, 1920, 751, 1922, 755, 779, 1950, 780, 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 1951, 1952, 1953, 1954, 1955, 791, 792, 793, 1956, 1957, 1958, 794, 795, 1959, 1960, 796, 1961, 797, 798, 799, 800, 801, 1962, 1963, 1964, 1965, 1966, 802, 803, 804, 805, 806, 1967, 1968, 1969, 1970, 1971, 807, 808, 809, 1972, 1973, 1974, 810, 811, 812, 1975, 1976, 1977, 813, 1978, 814, 815, 816, 817, 818, 1979, 1980, 1981, 1982, 1983, 819, 1984, 820, 821, 1985, 1986, 1987, 1988, 822, 823, 824, 1989, 1990, 1991, 1992, 1993, 1994, 825, 826, 827, 828, 1995, 829, 830, 1996, 1997, 831, 1998, 832, 1999, 833, 834, 835, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 836, 837, 838, 839, 840, 2007, 841, 2008, 2009, 842, 843, 2010, 2011, 844, 845, 2012, 2013, 846, 847, 2014, 848, 849, 850, 2015, 2016, 2017, 2018, 2019, 2020, 2021, 851, 852, 853, 854, 855, 856, 2022, 857, 858, 2023, 859, 860, 2024, 2025, 2026, 861, 2027, 2028, 862, 863, 864, 2029, 2030, 2031, 865, 866, 867, 868, 869, 870, 2032, 2033, 2034, 2035, 2036, 871, 2037, 2038, 2039, 872, 2040, 873, 874, 2041, 2042, 875, 876, 877, 2043, 2044, 2045, 878, 2046, 879, 880, 881, 2047, 2048, 882, 2049, 883, 884, 885, 2050, 2051, 2052, 2053, 886, 887, 2054, 2055, 888, 2056, 889, 2057, 2058, 890, 2059, 2060, 891, 892, 893, 894, 2061, 895, 896, 897, 2062, 2063, 898, 899, 2064, 2065, 2066, 2067, 2068, 2069, 900, 2070, 901, 902, 903, 2071, 2072, 2073, 2074, 904, 905, 906, 907, 2076, 908, 2077, 2078, 910, 911, 2079, 2080, 2082, 912, 2083, 913, 914, 915, 916, 2084, 917, 918, 2085, 2086, 2087, 919, 920, 921, 2088, 2089, 2090, 922, 923, 2091, 2092, 2093, 924, 2094, 925, 2095, 2096, 926, 2097, 927, 928, 2098, 929, 2099, 930, 2100, 931, 932, 2101, 933, 934, 935, 936, 937, 2102, 2103, 938, 2104, 2105, 2106, 2107, 2108, 2109, 939, 940, 941, 2110, 2111, 2112, 2113, 943, 944, 945, 2114, 2115, 2116, 946, 947, 948, 949, 950, 951, 952, 2117, 2118, 2119, 953, 954, 2120, 955, 956, 957, 2121, 2122, 2123, 958, 959, 960, 961, 962, 2124, 2125, 2126, 2127, 963, 964, 965, 966, 2128, 2129, 2130, 2131, 2132, 2133, 2134, 2135, 2136, 2137, 967, 2138, 968, 969, 970, 2139, 2140, 971, 972, 973, 974, 975, 2141, 2142, 2143, 2144, 2145, 2146, 976, 2147, 977, 978, 979, 980, 2148, 2149, 2150, 2151, 981, 982, 983, 2152, 2153, 2154, 984, 2155, 985, 2156, 986, 2157, 987, 2158, 988, 2159, 989, 990, 991, 992, 993, 994, 995, 2160, 2161, 2162, 2163, 2164, 2165, 997, 2166, 998, 999, 1000, 1001, 2168, 1002, 1003, 1004, 1005, 2169, 2170, 2171, 2172, 2173, 1006, 2174, 2175, 2176, 1007, 1008, 1009, 2177, 1010, 1011, 1012, 1013, 1014, 2178, 2179, 2180, 1015, 2181, 2182, 2183, 2184, 2185, 1016, 1017, 1018, 2186, 1019, 1020, 1021, 1022, 1023, 2187, 2188, 2189, 2190, 2191, 2192, 1024, 2193, 2194, 1025, 1026, 1027, 2195, 1028, 2196, 2197, 2198, 2199, 1029, 2200, 1030, 1031, 2201, 2202, 1032, 2203, 1033, 1034, 2204, 2205, 1035, 2206, 1036, 1037, 2207, 2208, 1038, 1039, 2209, 1040, 1041, 1042, 2210, 1043, 2211, 2212, 2213, 2214, 1044, 1045, 2215, 2216, 2217, 1048, 2218, 1049, 2219, 1050, 1051, 2220, 2221, 2222, 1052, 1053, 2223, 1054, 2224, 1055, 2225, 2226, 1056, 1057, 1058, 2227, 2228, 2229, 1059, 1060, 2230, 1061, 2231, 1062, 1063, 2232, 1064, 2233, 1065, 2234, 2235, 1066, 1067, 2236, 2237, 2238, 1068, 1069, 2239, 2240, 1070, 1071, 1072, 1073, 2241, 2242, 2243, 2244, 1074, 1075, 1076, 2245, 2246, 2247, 1077, 1078, 1079, 1080, 1081, 1082, 2248, 2249, 1083, 2250, 2251, 2252, 2253, 2254, 1085, 1086, 1087, 1088, 1089, 1090, 2255, 2256, 2257, 2258, 2259, 2260, 2261, 1091, 1093, 1094, 2262, 1095, 1096, 2263, 2264, 2265, 2266, 2267, 1097, 1098, 1099, 2268, 2269, 2270, 1100, 1101, 1102, 1103, 1104, 2271, 2272, 1105, 1106, 1107, 1108, 2273, 2274, 1109, 1110, 2275, 2276, 2277, 2278, 1111, 2279, 1112, 2280, 2281, 2282, 2283, 1113, 1114, 2284, 2285, 1115, 1116, 1117, 1118, 1119, 1120, 1121, 2286, 2287, 1122, 1123, 2288, 2289, 1124, 1125, 1126, 1127, 2290, 2291, 2292, 2293, 2294, 2295, 2296, 2297, 2298, 1128, 2299, 1129, 1130, 1131, 1132, 1133, 1134, 1135, 2300, 2301, 1136, 2302, 2303, 2304, 2305, 2306, 2307, 1137, 1138, 1139, 1140, 2309, 2310, 2315, 2316, 2317, 2318, 1143, 2319, 1144, 2320, 1145, 2321, 2322, 1146, 2323, 2324, 2325, 2326, 2327, 2328, 1147, 1148, 1149, 2329, 2330, 1150, 1151, 2331, 1152, 1153, 1154, 1155, 1156, 1157, 1158, 1159, 1160, 1161, 1162, 1164, 2332, 1165, 2333, 2334, 2335, 2336, 2337, 2338, 2339, 2340, 2341, 2342, 2343, 2344, 2345, 2346, 2347, 2348, 2349, 2350, 2351, 2352, 2353, 2354, 2355]

    # 最开始减少的cost和移动的百分比
    initial_cost_reduce = round(abs(cost0 -cost1 ) /cost0 ,2)
    initial_shift_reduce = round(abs(Ns0 -Ns1 ) /Ns0 ,2)
    # print(Ns1,cost1)
    # 调整比例
    diff_cost = 0.2   # 最终优化的和初始优化的cost的差值希望不超过20%
    diff_shift = 0.2   # 最终优化的和初始优化的shift的差值希望不超过15%
    x = 0.3  # 每次减少SRAM上x的数据
    # my_break = 0.0001   # 梯度下降中可以忍受的误差
    cost_reduce = initial_cost_reduce
    shift_reduce = initial_shift_reduce
    print("SRAM上的数：" ,data_SRAM)
    print("SRAM全部使用cost减少的百分比" ,initial_cost_reduce ,"SRAM全部使用shift减少的百分比" ,initial_cost_reduce ,"SRAM全部使用cost" ,cost1
      ,"SRAM全部使用shift" ,Ns1 ,"SRAM上的数据量" ,len(data_SRAM))

    data_SRAM2 = copy.deepcopy(data_SRAM)
    time_start = time.time()
    while initial_cost_reduce - cost_reduce < diff_cost and initial_shift_reduce - shift_reduce < diff_shift:
        cost2 ,energy2, Ns2, data_SRAM2 = reduce_sram(data_SRAM2, x, p0, access_sequence)
        cost_reduce = round(abs(cost0 - cost2) / cost0, 2)
        shift_reduce = round(abs(Ns0 - Ns2) / Ns0, 2)
        area = len(data_SRAM2 ) *As + (len(D) - len(data_SRAM2) ) *Ar
        print("cost减少的百分比：", cost_reduce, "shift减少的百分比：", shift_reduce, "延迟：", cost2, "移动次数：", Ns2, "SRAM上数据量：",
          len(data_SRAM2) ,"能耗" ,energy2 ,"总面积", area)
    # if initial_cost_reduce - cost_reduce < diff_cost and initial_shift_reduce - shift_reduce < diff_shift:
    #         #     continue
    #         # else:
    #         #     print("最后一组不满足条件")
    #         #     break
    time_end = time.time()
    print("time cost" ,time_end -time_start)
